package com.elmcode.server.service.impl;

import com.elmcode.server.dto.AddressDTO;
import com.elmcode.server.dto.EditAddressDTO;
import com.elmcode.server.elmenum.AddressStatusEnum;
import com.elmcode.server.exception.BusException;
import com.elmcode.server.interceptor.LoginInterceptor;
import com.elmcode.server.service.AddressService;
import com.elmcode.server.service.CustomerService;
import com.elmcode.server.vo.CustomerVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.Query;

import com.elmcode.server.dao.AddressDao;
import com.elmcode.server.entity.AddressEntity;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {

    @Autowired
    private CustomerService customerService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();
        String customerId = (String) params.get("customer_id");
        if(customerId != null){
            queryWrapper.eq("customer_id",customerId);
        }

        IPage<AddressEntity> page = this.page(
                new Query<AddressEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAddress(AddressDTO address) {
        CustomerVo customerVo = LoginInterceptor.threadLocal.get();

        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();

        QueryWrapper<AddressEntity> wrapper = queryWrapper.eq("customer_id", address.getCustomerId())
                .eq("province_name", address.getProvinceName())
                .eq("city_name", address.getCityName())
                .eq("area_name", address.getAreaName())
                .eq("detail", address.getDetail());

        AddressEntity addressEntity = this.baseMapper.selectOne(wrapper);
        if(addressEntity == null){
            //没有当前需要报错的地址，则保存
            AddressEntity entity = new AddressEntity();
            BeanUtils.copyProperties(address,entity);

            entity.setCustomerId(customerVo.getId());

            //处理默认地址
            if (address.getIsDefault()){
                entity.setIsDefault(AddressStatusEnum.IS_DEFAULT_ADDRESS.getCode());
                setDefaultAddress();
            }else {
                entity.setIsDefault(AddressStatusEnum.NO_DEFAULT_ADDRESS.getCode());
            }

            this.baseMapper.insert(entity);
        }else {
            throw new BusException("当前地址已经存在，操作失败");
        }
    }

    @Override
    public void updateAddress(Long id, EditAddressDTO address) {
        CustomerVo customerVo = LoginInterceptor.threadLocal.get();

        AddressEntity addressEntity = this.getById(id);

        addressEntity.setId(id);

        if(!StringUtils.isEmpty(address.getCityName())){
            addressEntity.setCityName(address.getCityName());
        }
        if(!StringUtils.isEmpty(address.getAreaName())){
            addressEntity.setAreaName(address.getAreaName());
        }
        if(!StringUtils.isEmpty(address.getProvinceName())){
            addressEntity.setProvinceName(address.getProvinceName());
        }
        if(!StringUtils.isEmpty(address.getDetail())){
            addressEntity.setDetail(address.getDetail());
        }
        if(address.getIsDefault() != null){

            if(address.getIsDefault()){
                setDefaultAddress();
                addressEntity.setIsDefault(AddressStatusEnum.IS_DEFAULT_ADDRESS.getCode());
            }else {
                addressEntity.setIsDefault(AddressStatusEnum.NO_DEFAULT_ADDRESS.getCode());
            }
        }

        this.baseMapper.updateById(addressEntity);
    }

    @Override
    public AddressEntity getDefaultAddr() {
        CustomerVo customerVo = LoginInterceptor.threadLocal.get();

        //拿到当前顾客的默认地址
        AddressEntity selectOne = this.baseMapper.selectOne(new QueryWrapper<AddressEntity>().eq("customer_id", customerVo.getId())
                .eq("is_default", AddressStatusEnum.IS_DEFAULT_ADDRESS.getCode()));

        return selectOne;
    }

    private void setDefaultAddress() {
        //拿到当前顾客的默认地址
        AddressEntity selectOne = getDefaultAddr();
        if(selectOne != null){
            this.baseMapper.updateById(selectOne);
        }
    }

}